/* *****************************************************************************************
 
Made by www.Flashphoner.com, all rights reserved.
These sources can only be used for working with Flashphoner server.
If you want use this to create your own application (separate from Flashphoner server) � 
please contact support@flashphoner.com 

******************************************************************************************/

package com.flashphoner.api
{
		
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.flashphoner.Logger;
	import com.flashphoner.api.data.ModelLocator;
	import com.flashphoner.api.data.PhoneConfig;
	import com.flashphoner.api.management.SoundControl;
	import com.flashphoner.api.management.VideoControl;
	
	import flash.events.*;
	import flash.utils.*;
	
	internal class CallCommand implements ICommand
	{		
		private var hangupTimer:Timer;
		
		public function CallCommand()
		{
		}	
				
		public function execute( event : CairngormEvent ) : void
		{	
			Logger.info("PhoneCommand.execute() event.type "+event.type);
			
			var call:Call = (event as CallEvent).call;
			var flashAPI:Flash_API = call.flash_API;
			var modelLocator:ModelLocator = flashAPI.modelLocator;
			
			if (event.type==CallEvent.TALK){
				Logger.info("MainEvent.TALK "+call.id);
				SoundControl.getInstance().stopRingSound();				

				call.startTimer();
				call.publish();						
				flashAPI.phoneServerProxy.phoneSpeaker.play("INCOMING_"+modelLocator.login+"_"+call.id);				
				if (PhoneConfig.VIDEO_ENABLED){
					flashAPI.phoneServerProxy.phoneSpeaker.playVideo("VIDEO_INCOMING_"+modelLocator.login+"_"+call.id);
				}
			}
			
			if (event.type==CallEvent.HOLD){
				call.unpublish();
			}
			 
			if (event.type == CallEvent.SESSION_PROGRESS){
				Logger.info("MainEvent.SESSION_PROGRESS");
		 		SoundControl.getInstance().stopRingSound();
				
				call.publish();	 		
		 		flashAPI.phoneServerProxy.phoneSpeaker.play("INCOMING_"+modelLocator.login+"_"+call.id);
		 	}
			
			if (event.type==CallEvent.IN){
				SoundControl.getInstance().playRingSound();
			}
			
			if (event.type ==CallEvent.OUT){
				SoundControl.getInstance().playRingSound();
			}
			
			if (event.type == MainEvent.VIDEO_FORMAT_CHANGED){
				
				if (call.streamerVideoFormat!=null){
					VideoControl.getInstance().changeFormat(call.streamerVideoFormat.toLowerCase());
				}
				
				if (call.playerVideoFormat!=null) {
					flashAPI.phoneServerProxy.phoneSpeaker.changeFormat(call.playerVideoFormat.toLowerCase());
				}
			}			
			
			if (event.type == CallEvent.BUSY || event.type == CallEvent.FINISH){
				SoundControl.getInstance().playBusySound();		
				SoundControl.getInstance().stopRingSound();														

				call.stopTimer();
				call.unpublish();	
				flashAPI.removeCall(call.id);
				flashAPI.phoneServerProxy.phoneSpeaker.stopAudio(call.id);
				flashAPI.phoneServerProxy.phoneSpeaker.stopVideo(call.id);
			}				
		}
	}
}